package com.trust.pams.controller;

import com.chasonx.common.interceptor.ParamData;
import com.chasonx.common.model.RpcResult;
import com.chasonx.common.util.Kit;
import com.chasonx.common.util.Ret;
import com.trust.pams.model.query.PamsClientQuery;
import com.trust.pams.model.query.PamsMilestoneQuery;
import com.trust.pams.model.query.PamsQuoteQuery;
import com.trust.pams.model.query.PamsUserQuery;
import com.trust.pams.service.read.PamsMilestoneRead;
import com.trust.pams.service.read.PamsQuoteRead;
import com.trust.pams.service.read.PamsUserRead;
import com.trust.pams.service.transactional.PamsQuoteTransactional;
import com.trust.pams.util.UUIDUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@RestController
@RequestMapping("/pams/quote")
@Api(value = "quote")
public class TrutsPamsQuoteController {

    @Autowired
    private PamsQuoteTransactional quoteTransactional;


    @Autowired
    private PamsQuoteRead read;

    @Autowired
    private PamsUserRead userRead;

    @Autowired
    private PamsMilestoneRead pamsMilestoneRead;
    @Value("${tmp.path}")
    private String tmp_path;

    @ApiOperation(value = "导入项目报价表", notes = "项目报表导入")
    @RequestMapping(value = "improtQuote", method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "file", value = "文件地址", paramType = "form", dataType = "file", required = true)
    })
    public Ret improtQuote(@RequestParam("file") MultipartFile xlsFile){
        Ret ret= Ret.get();
        if(!xlsFile.getOriginalFilename().endsWith(".xlsx") && !xlsFile.getOriginalFilename().endsWith(".xls")) {
            ret.setMsg("请上传excel文件后再试!");
            return ret;
        }
        String ext = xlsFile.getOriginalFilename().substring(xlsFile.getOriginalFilename().lastIndexOf("."));
        String excelPath=tmp_path+File.separator+ UUIDUtil.generateUUID()+ext;
        try {
            xlsFile.transferTo(new File(excelPath));
        }catch(IOException e){
            ret.setMsg("文件转换失败");
        }
        ret = quoteTransactional.batchPamsQuote(excelPath);
        return ret;
    }


    @ApiOperation(value = "导入项目客户表", notes = "项目客户信息导入")
    @RequestMapping(value = "improtClient", method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "file", value = "文件地址", paramType = "form", dataType = "file", required = true)
    })
    public Ret improtClient(@RequestParam("file") MultipartFile xlsFile){
        Ret ret= Ret.get();
        if(!xlsFile.getOriginalFilename().endsWith(".xlsx") && !xlsFile.getOriginalFilename().endsWith(".xls")) {
            ret.setMsg("请上传excel文件后再试!");
            return ret;
        }
        String ext = xlsFile.getOriginalFilename().substring(xlsFile.getOriginalFilename().lastIndexOf("."));
        String excelPath=tmp_path+File.separator+ UUIDUtil.generateUUID()+ext;
        try {
            xlsFile.transferTo(new File(excelPath));
        }catch(IOException e){
            ret.setMsg("文件转换失败");
        }
        ret = quoteTransactional.batchPamsClient(excelPath);
        return ret;
    }

    @ApiOperation(value = "查询项目报价表", notes = "项目报表导入")
    @RequestMapping(value = "listPamsQute", method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "projectName", value = "项目名称",required = false),
            @ApiImplicitParam(name = "pageNumber", value = "页码", required = false),
            @ApiImplicitParam(name = "pageSize", value = "页量", required = false)
    })
    public Ret listPamsQute(@RequestBody PamsQuoteQuery quoteQueryParamData){
        Ret result = read.ListPamsQuote(quoteQueryParamData);
        return result;
    }

    @ApiOperation(value = "查询最近项目", notes = "项目报表导入")
    @RequestMapping(value = "listPamsQuteLast", method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "projectName", value = "项目名称",required = false),
            @ApiImplicitParam(name = "pageNumber", value = "页码", required = false),
            @ApiImplicitParam(name = "pageSize", value = "页量", required = false)
    })
    public Ret listPamsQuteLast(@RequestBody PamsQuoteQuery quoteQueryParamData){
        Ret result = read.ListPamsQuotelast(quoteQueryParamData);
        return result;
    }

    @ApiOperation(value = "查询项目客户表", notes = "项目客户导入")
    @RequestMapping(value = "listPamsClient", method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "clientName", value = "客户名称",required = false),
            @ApiImplicitParam(name = "pageNumber", value = "页码", required = false),
            @ApiImplicitParam(name = "pageSize", value = "页量", required = false)
    })
    public Ret listPamsClient(@RequestBody PamsClientQuery pamsClientQuery){
        Ret result = read.ListPamsClient(pamsClientQuery);
        return result;
    }

    @ApiOperation(value = "全局查询项目报价表", notes = "项目报表导入")
    @RequestMapping(value = "listPamsQutes", method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "projectName", value = "项目名称",required = false),
            @ApiImplicitParam(name = "pageNumber", value = "页码", required = false),
            @ApiImplicitParam(name = "pageSize", value = "页量", required = false)
    })
    public Ret listPamsQutes(@RequestBody PamsQuoteQuery quoteQueryParamData){
        Ret result = read.ListPamsQuotes(quoteQueryParamData);
        return result;
    }

    @ApiOperation(value = "根据id查询项目详情", notes = "项目报表导入")
    @RequestMapping(value = "getPamsQute", method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "文件地址", paramType = "form", dataType = "file", required = true)
    })
    public Ret getPamsQute(@RequestParam("id") String id){
        Ret result = read.getPamsQuote(id);
        return result;
    }

    @ApiOperation(value = "根据id查询项目图形", notes = "项目报表导入")
    @RequestMapping(value = "getAll", method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "项目", paramType = "form", dataType = "String", required = true)
    })
    public Ret getAll(@RequestParam("id") String id,String isitAll){
        Ret result = read.getAll(id,isitAll);
        return result;
    }

    @ApiOperation(value = "根据名称查看进度", notes = "项目报表导入")
    @RequestMapping(value = "getNameMilestone", method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "人员名称", paramType = "form", dataType = "String", required = true)
    })
    public Ret getNameMilestone(String key,String name,String type,String projectId,String creationTime){
        Ret result = read.getNameMilestone(key,name,type,projectId,creationTime);
        return result;
    }

    @ApiOperation(value = "根据名称显示关联项目", notes = "项目报表导入")
    @RequestMapping(value = "getMilestone", method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "人员名称", paramType = "form", dataType = "String", required = true)
    })
    public Ret getMilestone(String key,String name){
        Ret result = read.getMilestone(key,name);
        return result;
    }

    @ApiOperation(value = "根据名称显示进度图形", notes = "项目报表导入")
    @RequestMapping(value = "getPlan", method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "人员名称", paramType = "form", dataType = "file", required = true)
    })
    public Ret getPlan(String key,String name){
        Ret result = read.getPlan(key,name);
        return result;
    }


    @ApiOperation(value = "根据名称显示进度图形", notes = "项目报表导入")
    @RequestMapping(value = "getUser", method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "人员名称", paramType = "form", dataType = "String", required = true)
    })
    public Ret getUser(String name){
        Ret result = read.getUers(name);
        return result;
    }

//    @ApiOperation(value = "根据id查询展开图形", notes = "项目报表导入")
//    @RequestMapping(value = "checkAll", method = RequestMethod.POST)
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "id", value = "文件地址", paramType = "form", dataType = "file", required = true)
//    })
//    public Ret checkAll(@RequestParam("id") String id){
//        Ret result = read.checkAll(id);
//        return result;
//    }

    @ApiOperation(value = "导入项目用户表", notes = "项目客户信息导入")
    @RequestMapping(value = "improtPamsUser", method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "file", value = "文件地址", paramType = "form", dataType = "file", required = true)
    })
    public Ret improtPamsUser(@RequestParam("file") MultipartFile xlsFile){
        Ret ret= Ret.get();
        if(!xlsFile.getOriginalFilename().endsWith(".xlsx") && !xlsFile.getOriginalFilename().endsWith(".xls")) {
            ret.setMsg("请上传excel文件后再试!");
            return ret;
        }
        String ext = xlsFile.getOriginalFilename().substring(xlsFile.getOriginalFilename().lastIndexOf("."));
        String excelPath=tmp_path+File.separator+ UUIDUtil.generateUUID()+ext;
        try {
            xlsFile.transferTo(new File(excelPath));
        }catch(IOException e){
            ret.setMsg("文件转换失败");
        }
        ret = quoteTransactional.batchPamsUser(excelPath);
        return ret;
    }

    @ApiOperation(value = "查询用户信息", notes = "用户报表导入")
    @RequestMapping(value = "listPamsUser", method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "用户名称",required = false),
            @ApiImplicitParam(name = "pageNumber", value = "页码", required = false),
            @ApiImplicitParam(name = "pageSize", value = "页量", required = false)
    })
    public Ret listPamsUser(@RequestBody PamsUserQuery pamsUserQuery){
        Ret result = userRead.ListPamsUser(pamsUserQuery);
        return result;
    }


    @ApiOperation(value = "导入项目里程碑", notes = "项目里程碑信息导入")
    @RequestMapping(value = "improtPamsMilestone", method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "file", value = "文件地址", paramType = "form", dataType = "file", required = true)
    })
    public Ret improtPamsMilestone(@RequestParam("file") MultipartFile xlsFile){
        Ret ret= Ret.get();
        if(!xlsFile.getOriginalFilename().endsWith(".xlsx") && !xlsFile.getOriginalFilename().endsWith(".xls")) {
            ret.setMsg("请上传excel文件后再试!");
            return ret;
        }
        String ext = xlsFile.getOriginalFilename().substring(xlsFile.getOriginalFilename().lastIndexOf("."));
        String excelPath=tmp_path+File.separator+ UUIDUtil.generateUUID()+ext;
        try {
            xlsFile.transferTo(new File(excelPath));
        }catch(IOException e){
            ret.setMsg("文件转换失败");
        }
        ret = quoteTransactional.batchPamsMilestone(excelPath);
        return ret;
    }

    @ApiOperation(value = "查询项目里程碑", notes = "项目里程碑报表导入")
    @RequestMapping(value = "listPamsMilestone", method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "projectId", value = "项目编号",required = false),
            @ApiImplicitParam(name = "pageNumber", value = "页码", required = false),
            @ApiImplicitParam(name = "pageSize", value = "页量", required = false)
    })
    public Ret listPamsMilestone(@RequestBody PamsMilestoneQuery pamsMilestoneQuery){
        Ret result = pamsMilestoneRead.ListPamsMilestone(pamsMilestoneQuery);
        return result;
    }
}
